package ;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.GlowFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextFormat;


import particles.EffectsLayer;
import particles.Emitter;

import Movable;

/**
 * ...
 * @author 
 */

class JagSceneView extends JagScene
{
	private static var SceneBackground1 = 100;

	var lBack: Sprite;
	var lWall: Sprite;
	var lItem: Sprite;
	
	var lEffects: EffectsLayer;

	
	public function new() 
	{
		super();

		// frames between logic updates
		mUpdateSpeed = 8;
	}
	
	
	override public function LoadResources()
	{
		//trace("LoadResources");
		
		AddSprite(SceneBackground1, "gfx/Fon2.png");
		
		AddSprite(JagTileInfo.Background1, "gfx/Background1.png");
		AddSprite(JagTileInfo.Wall1, "gfx/Wall1.png");
		
		AddSprite(JagTileInfo.Item1, "gfx/Item1.png");
		AddSprite(JagTileInfo.Item2, "gfx/Item2.png");
		AddSprite(JagTileInfo.Item3, "gfx/Item3.png");
		AddSprite(JagTileInfo.Item4, "gfx/Item4.png");
		AddSprite(JagTileInfo.Item5, "gfx/Item5.png");
		AddSprite(JagTileInfo.Item6, "gfx/Item6.png");
		
		//AddSprite(TileInfo.Target1, "gfx/target1.png");
		//AddSprite(TileInfo.Target2, "gfx/target2.png");
	}	
		
		
	override function CreateHud()
	{
		var cScore = new CounterView(this, mBackground.width - 108, 32, mCounters.Score);
		cScore.SetTextFormat(new TextFormat("Arial", 18, 0xffff00, false, false, false));
		cScore.SetMaxChars(6);
		
		var cScoreBonus = new CounterView(this, mBackground.width - 40, 12, mCounters.ScoreBonus);
		cScoreBonus.SetTextFormat(new TextFormat("Arial", 12, 0xff3300, true, false, false));
		cScoreBonus.SetStringFormat("x%%");
		
		var cTime = new TimeCounterView(this, mBackground.width - 108, 90, mCounters.Time);
		cTime.SetTextFormat(new TextFormat("Arial", 18, 0xffff00, false, false, false));
		
		for (i in 0...mMaxItems) {
			var cItem = new CounterView(this, mXoff + i * 104, mBackground.height - 70, mCounters.Items[i]);
			cItem.SetMaxChars(5);
			cItem.SetIcon(Resources.Inst.GetSprite(JagTileInfo.Item1 + i));
			cItem.SetTextFormat(new TextFormat("Arial", 16, 0x663300, true, false, false));
		}
	}
	
	
	override function CreateField()
	{
		super.CreateField();
		
		//trace("JagSceneView::CreateField");
		
		SetTileSize(32, 32);
		SetFieldOffset(10, 10);	
		
		
		// game layers
		lBack = new Sprite();
		addChild(lBack);

		lWall = new Sprite();
		addChild(lWall);

		lItem = new Sprite();
		addChild(lItem);	
	
		
		// effects layer
		lEffects = new EffectsLayer(width, height);
		addChild(lEffects);
		
		lEffects.filter = new GlowFilter(0xFF9933, 1, 10, 10);
	}
	
	
	override function GenerateLevel()
	{
		//trace("JagSceneView::GenerateLevel");
				
		SetBackground(SceneBackground1);
				
		super.GenerateLevel();
	}	
	
	
	override private function SetBackgroundTile(pos: SceneCoord2d, typeInfo: Int)
	{
		super.SetBackgroundTile(pos, typeInfo);
		
		// GUI
		var tile = new MovableSprite(typeInfo, GetViewPos(pos));
		lBack.addChild(tile);
	}

	
	override private function SetWallTile(pos: SceneCoord2d, typeInfo: Int)
	{
		super.SetWallTile(pos, typeInfo);
		
		// GUI
		var tile = new MovableSprite(typeInfo, GetViewPos(pos));
		lWall.addChild(tile);
	}
	
	
	override private function CreateNewItem(pos: SceneCoord2d)
	{
		super.CreateNewItem(pos);
		
		// GUI
		var tile: JagTile = new JagTile(GetInfoPos(pos), GetViewPos(pos));
		tile.name = pos.toString();		
		
		lItem.addChild(tile);
		AddGuiObject(tile);
	}
	
	
	override private function KillItem(pos: SceneCoord2d)
	{
		super.KillItem(pos);
		
		var viewPos = GetViewPos(pos);
		
		// create ghost objects
		var itemInfo = GetInfoPos(pos);
		
		var ghost = new MovableSprite(itemInfo.GetId(), viewPos);
		
		ghost.SetLifeTime(100);										// live 100 frames
		ghost.SetSpeed((Math.random()*4-2)/2, Math.random()*4+1);	// move down
		ghost.SetBlending(1, 0, 100);								// blend out
		
		lItem.addChild(ghost);		
		AddGuiObject(ghost);
		
		// create particles
		
		var tw2 = mTileWidth / 2;
		var th2 = mTileHeight / 2;
		viewPos.add(new Point(tw2, th2));
		
		var e = new Emitter(lEffects, viewPos, tw2, th2);
		e.SetLifeTime(200);
		e.SetPartCount(20);
		e.SetPartDeltaX( -1, 1);
		e.SetPartDeltaY(-2, -0.5);
		e.SetPartGravityX(0, 0);
		e.SetPartGravityY(0, 0.05);
		e.SetPartSize(0.5, 1.5);
		e.SetPartTrailing(6);
		e.SetPartColors(0x00ffff, 0x0000ff);
		
		lEffects.addChild(e);
		AddGuiObject(e);
	}
	
	
	override private function RemoveItem(pos: SceneCoord2d)
	{
		super.RemoveItem(pos);
		
		var item: Movable = cast(lItem.getChildByName(pos.toString()), Movable);
		if (item != null) {
			item.SetLifeTime(0);
		}
	}
	
	
	override private function SwapItems(pos1: SceneCoord2d, pos2: SceneCoord2d)
	{
		super.SwapItems(pos1, pos2);
		
		var itemInfo1 = GetInfoPos(pos1);
		var itemInfo2 = GetInfoPos(pos2);
		
		var item1: Movable = cast(lItem.getChildByName(pos1.toString()), Movable);
		var item2: Movable = cast(lItem.getChildByName(pos2.toString()), Movable);
		
		if (item1 != null) {
			item1.MoveTo(GetViewPos(pos2), mUpdateSpeed * JagTileInfo.MoveTime);
			item1.name = pos2.toString();
		}

		if (item2 != null) {
			item2.MoveTo(GetViewPos(pos1), mUpdateSpeed * JagTileInfo.MoveTime);
			item2.name = pos1.toString();
		}
	}
	
	
	override private function MoveItem(posFrom: SceneCoord2d, posTo: SceneCoord2d)
	{
		super.MoveItem(posFrom, posTo);
		
		var itemInfo = GetInfoPos(posFrom);
		var item: Movable = cast(lItem.getChildByName(posFrom.toString()), Movable);
		if (item != null) {
			item.MoveTo(GetViewPos(posTo), mUpdateSpeed * JagTileInfo.MoveTime);
			item.name = posTo.toString();
		}
	}
		
	
	override function Render(inExtra:Float)
	{
		lEffects.EnterFrame();
				
		super.Render(inExtra);
		
		//var filter: GlowFilter = cast(lEffects.filter, GlowFilter);
		//filter.color = Std.int(Math.random() * 0xFFFFFF);
				
		lEffects.FinishFrame();
	}	

}